package com.example.qixin0314.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qixin0314.R;

import org.jetbrains.annotations.NotNull;

import cn.leancloud.LCUser;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = LoginActivity.class.getSimpleName();
    ImageView go_back;
    Button register;
    EditText edit_phone;//用户名
    EditText edit_mima;//密码
    Button butt;//登录
    TextView WJpads;//忘记密码
    TextView phone_login;//手机号登录

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();//控件初始化
        Ago_back();//退出当前页面
        Aregister();//去注册
        Abutt();//登录
        Aphone_login();//手机号登录
        AWJpads();//忘记密码



    }

    private void initView() {
        go_back=findViewById(R.id.go_back);
        register=findViewById(R.id.register);
        edit_phone=findViewById(R.id.edit_phone);
        edit_mima=findViewById(R.id.edit_mima);
        butt=findViewById(R.id.butt);
        WJpads=findViewById(R.id.WJpads);
        phone_login=findViewById(R.id.phone_login);
        Intent intent3 = getIntent();
        if (intent3.getExtras() != null) {
            String phone = intent3.getExtras().getString("phone");
            edit_phone.setText(phone);
        }
    }

    private void Ago_back(){
        go_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,OneActivity.class);
                startActivity(intent);
            }
        });
    }

    private void Aregister(){
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

    }

    private void Abutt(){
        butt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edit_phone.getText().toString();
                String psds = edit_mima.getText().toString();
                if (TextUtils.isEmpty(name)){
                    Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(psds)){
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                LCUser.logIn(name,psds)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCUser>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) { }

                    @Override
                    public void onNext(@NotNull LCUser lcUser) {
                        Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(LoginActivity.this,OneActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {
                        Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

            }
        });



    }

    private void Aphone_login(){
        phone_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,PhoneLoginActivity.class);
                startActivity(intent);
            }
        });

    }

    private void AWJpads(){
        WJpads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,WJpsdsActivity.class);
                startActivity(intent);
            }
        });
    }
}